package com.example.mall.web.controller;

import com.example.mall.domain.Role;
import com.example.mall.dto.PermissionDto;
import com.example.mall.dto.RoleDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.PageDTO;
import com.example.mall.query.RoleQuery;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    public IRoleService roleService;

    /**
     * 分页查询登录角色表信息
     * @param roleQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询登录角色表信息",notes = "分页查询登录角色表信息",httpMethod = "GET")
    public JSONResult queryPage(RoleQuery roleQuery){
        PageDTO<RoleDto> roleDtoPageDTO = roleService.queryPage(roleQuery);
        return JSONResult.success("查询成功",roleDtoPageDTO);
    }

    /**
     * 根据id查询登录角色表信息
     * @param roleId
     * @return
     */
    @GetMapping("query/{roleId}")
    @ApiOperation(value = "根据id查询登录角色表信息",notes = "根据id查询登录角色表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("roleId")Long roleId){
        //1.根据id查询用户信息
        if (roleId == null){
            throw new GlobalCustomerException(ErrorEnumCode.ROLEID_NOT_ID);
        }
        Role role = roleService.getById(roleId);
        return JSONResult.success("查询成功",role);
    }

    /**
     * 根据id查询登录角色表信息
     * @param roleId
     * @return
     */
    @GetMapping("queryRoleById/{roleId}")
    @ApiOperation(value = "根据id查询登录角色表信息",notes = "根据id查询登录角色表信息",httpMethod = "GET")
    public JSONResult queryRoleById(@PathVariable("roleId")Long roleId){
        //1.根据id查询用户信息
        if (roleId == null){
            throw new GlobalCustomerException(ErrorEnumCode.ROLEID_NOT_ID);
        }
        Role role = roleService.queryRoleById(roleId);
        return JSONResult.success("查询成功",role);
    }

    /**
     * 查询全部登录角色表信息
     * @param
     * @return
     */
    @GetMapping("queryRoleList")
    @ApiOperation(value = "查询全部登录角色表信息",notes = "查询全部登录角色表信息",httpMethod = "GET")
    public JSONResult queryRoleList(){
        List<Role> roles = roleService.queryRoleList();
        return JSONResult.success("查询成功",roles);
    }

    /**
     * 新增登录角色表
     * @param roleDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增登录角色表",notes = "新增登录角色表",httpMethod = "POST")
    public JSONResult insert(@RequestBody RoleDto roleDto){
        roleService.insert(roleDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改登录角色表信息
     * @param roleDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改登录角色表信息",notes = "修改登录角色表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody RoleDto roleDto){
        roleService.update(roleDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除登录角色表信息
     * @param roleId
     * @return
     */
    @PostMapping("delete/{roleId}")
    @ApiOperation(value = "删除登录角色表信息",notes = "删除登录角色表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("roleId") Long roleId) {
        roleService.delete(roleId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除登录角色表信息
     * @param roleIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除登录角色表信息",notes = "批量删除登录角色表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> roleIds) {
        roleService.deleteBatch(roleIds);
        return JSONResult.success("批量删除成功");
    }
}
